package com.basic.controller;


import com.basic.entity.User;
import com.basic.service.UserService;
import com.basic.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 第五组
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/userAll")
    public List<User> userAll(){
        List<User> users = userService.selectAll();
        return users;
    }

    @RequestMapping("/userSelectById/{userId}")
    public User selectById(@PathVariable("userId")Integer userId){
        User user = userService.selectById(userId);
        return user;
    }

    @RequestMapping("/userAdd")
    public String userList(@RequestBody UserVo userVo){
        return userService.addUser(userVo);
    }

    @RequestMapping("/userTransfer")
    public String transfer(@RequestParam("out") String out,
                           @RequestParam("in") String in,
                           @RequestParam("balance") Double balance) {
        try {
            userService.transfer(out,in,balance);
            return "转账成功";
        } catch (Exception e) {
            // 这里可以记录日志，并且根据不同的异常返回不同的错误信息
            return "转账失败: " + e.getMessage();
        }
    }
}
